<?php

namespace addons\sum\controller\goods;

use addons\sum\controller\Common;
use addons\sum\service\goods\GoodsService;
use app\admin\model\sum\store\activity\Activity as StoreActivityModel;
use app\admin\model\sum\store\activity\SkuPrice as ActivitySkuPriceModel;
use app\admin\model\sum\store\Store as StoreModel;
use app\admin\model\sum\store\StoreGoods as StoreGoodsModel;

class StoreGoods extends Common
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 商品列表
     * @return void
     */
    public function index(){
        $params = $this->request->param();
        $store_ids=$params['store_ids']??'';
        if(!$store_ids){
            $store_ids='5';//默认的店铺id-方便审核测试
//            $this->error('参数错误');
        }
        $store_ids_arr=explode(',',$store_ids);

        $is_new = $params['is_new'] ?? false;
        $is_exquisite = $params['is_exquisite'] ?? false;
        $is_recommend = $params['is_recommend'] ?? false;
        $keyword = $params['keyword'] ?? '';
        if($keyword){
            $keyword = is_string($keyword) ? $keyword : json_encode($keyword);      // 转字符串
        }

        $category_id = $params['category_id'] ?? '';

        $sort = $params['sort'] ?? 'weigh';
        $order = $params['order'] ?? 'desc';


        $recodes = StoreGoodsModel::hasWhere('goods',function ($q) use($is_new,$is_exquisite,$is_recommend,$keyword,$category_id){
                if($is_new){
                    $q=$q->where('is_new',1);
                }
                if($is_exquisite){
                    $q=$q->where('is_exquisite',1);
                }
                if($is_recommend){
                    $q=$q->where('is_recommend',1);
                }
                if($keyword){
                    $q=$q->where('title|subtitle', 'like', '%' . $keyword . '%');
                }
                if ($category_id) {
                    $q=$q->where('category_ids',$category_id);
                }
                $q=$q->where('Goods.status','up');
                return $q;
            })->with(['goods'])
            ->whereIn('store_id',$store_ids_arr)
            ->order($sort, $order)
            ->paginate();

        $this->success('获取成功', $recodes);



    }




}
